﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Multiprocessing.Core.Algorithm;
using Multiprocessing.Core.Units;

namespace Multiprocessing.Analyse
{
    public sealed class Acceleration : BaseAnalyseUnit
    {
        #region Properties
        
        public Acceleration(IAlgorithm algorithm) 
            : base(algorithm)
        {
        }

        #endregion

        public override double Calculate(int processingUnitCount, params object[] args)
        {
            var singleUnitResult = this.Algorithm.Execute<TaskProcessingBlock>(args);
            var multResult = this.Algorithm.Execute<TaskProcessingBlock>(processingUnitCount, args);
            return singleUnitResult.ElapsedTime.TotalMilliseconds/multResult.ElapsedTime.TotalMilliseconds;
        }

        public override double Calculate(int processingUnitCount, Type processingBlockType, out TimeSpan elapsed, params object[] args)
        {
            var singleUnitResult =
                this.Algorithm.Execute(ProcessingBlockFactory.Instance.CreateProcessingBlock(processingBlockType, 1),
                                       args);
            var multResult =
                this.Algorithm.Execute(
                    ProcessingBlockFactory.Instance.CreateProcessingBlock(processingBlockType, processingUnitCount),
                    args);
            elapsed = multResult.ElapsedTime;
            return singleUnitResult.ElapsedTime.TotalMilliseconds / multResult.ElapsedTime.TotalMilliseconds;
        }
    }
}
